﻿using System;
using System.Linq;
using System.Text;
using Biz.Buffer;
using Biz.EF.Platform;
using Hjt212Parser;
using log4net;

namespace Biz.Process
{
    public class UplineFromPkscForMonitor
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(UplineFromPkscForMonitor));
        public static void SetStatus(string content, bool isOnline)
        {
            try
            {
                var mn = Parser.GetStringByKey("MN", content);
                if (String.IsNullOrWhiteSpace(mn)) return;
                var mapping = CommonInformation.GetTableMappingByPkscMn(mn);
                if (mapping == null) return;
                using (var ctx = new PlatformContext())
                {
                    var monitor = ctx.RealDataMonitorMains.FirstOrDefault(t => t.企业基本信息ID == mapping.enterprise_id && t.设备信息ID == mapping.port_id) ??
                                  new RealDataMonitorMain();
                    monitor.企业基本信息ID = mapping.enterprise_id;
                    monitor.设备信息ID = mapping.port_id;
                    monitor.污染物类别ID = 2;
                    monitor.在线状态ID = isOnline ? 601 : 602;
                    monitor.在离线状态时间 = Parser.GetDateTimeByDataTimeThenQnThenNow(content);
                    ctx.SaveChanges();
                }
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
        }
        /// <summary>
        /// 处理2011指令
        /// </summary>
        /// <param name="content"></param>
        public static void Process2011(string content)
        {
            try
            {
                var mn = Parser.GetStringByKey("MN", content);
                if (String.IsNullOrWhiteSpace(mn)) return;
                var mapping = CommonInformation.GetTableMappingByPkscMn(mn);
                if (mapping == null) return;
                using (var ctx = new PlatformContext())
                {
                    var monitorMain = ctx.RealDataMonitorMains.FirstOrDefault(t => t.企业基本信息ID == mapping.enterprise_id && t.设备信息ID == mapping.port_id) ;
                    if (monitorMain == null)
                    {
                        monitorMain =new RealDataMonitorMain();
                        ctx.RealDataMonitorMains.Add(monitorMain);
                    }
                    monitorMain.企业基本信息ID = mapping.enterprise_id;
                    monitorMain.设备信息ID = mapping.port_id;
                    monitorMain.污染物类别ID = 2;
                    if (monitorMain.在线状态ID == null || monitorMain.在线状态ID != 601)
                    {
                        monitorMain.在线状态ID = 601;
                        monitorMain.在离线状态时间 = Parser.GetDateTimeByDataTimeThenQnThenNow(content);
                    }

                    float? masterFactor = null;

                    foreach (var key in MasterFactors.Factors)
                    {
                        masterFactor = Parser.GetFloatByKey(key + "-Rtd", content);
                        if (masterFactor != null)
                        {
                            break;
                        }
                    }

                    monitorMain.瞬时量 = masterFactor;
                    monitorMain.实时数据时间 = Parser.GetDateTimeByKey("DataTime", content);

                    ctx.SaveChanges();
                    var factors = Parser.GetPollutantCodes(content);
                    foreach (var key in MasterFactors.Factors)
                    {
                        factors.Remove(key);
                    }

                    foreach (var factor in factors)
                    {
                        var factorCode = CommonInformation.GetMonitorFactorCodes(factor);
                        if (factorCode == null) continue;
                        var monitorDetail = monitorMain.RealDataMonitorDetails.FirstOrDefault(t => t.污染物编码ID == factorCode.ID) ;
                        if (monitorDetail == null)
                        {
                            monitorDetail = new RealDataMonitorDetail();
                            monitorMain.RealDataMonitorDetails.Add(monitorDetail);
                        }
                        monitorDetail.主实时监控ID = monitorMain.ID;
                        monitorDetail.污染物编码ID = factorCode.ID;
                        monitorDetail.瞬时量 = Parser.GetFloatByKey(factor + "-Rtd", content);
                    }
                    ctx.SaveChanges();
                }
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
        }
        public static void Process2051(string content)
        {
            try
            {
                var mn = Parser.GetStringByKey("MN", content);
                if (String.IsNullOrWhiteSpace(mn)) return;
                var mapping = CommonInformation.GetTableMappingByPkscMn(mn);
                if (mapping == null) return;
                using (var ctx = new PlatformContext())
                {
                    var monitorMain = ctx.RealDataMonitorMains.FirstOrDefault(t => t.企业基本信息ID == mapping.enterprise_id && t.设备信息ID == mapping.port_id) ;
                    if (monitorMain == null)
                    {
                        monitorMain = new RealDataMonitorMain();
                        ctx.RealDataMonitorMains.Add(monitorMain);
                    }
                    monitorMain.企业基本信息ID = mapping.enterprise_id;
                    monitorMain.设备信息ID = mapping.port_id;
                    monitorMain.污染物类别ID = 2;
                    if (monitorMain.在线状态ID == null || monitorMain.在线状态ID != 601)
                    {
                        monitorMain.在线状态ID = 601;
                        monitorMain.在离线状态时间 = Parser.GetDateTimeByDataTimeThenQnThenNow(content);
                    }

                    float? masterFactor = null;

                    foreach (var key in MasterFactors.Factors)
                    {
                        masterFactor = Parser.GetFloatByKey(key + "-Cou", content);
                        if (masterFactor != null)
                        {
                            break;
                        }
                    }

                    monitorMain.分钟排放量 = masterFactor;
                    monitorMain.分钟数据时间 = Parser.GetDateTimeByKey("DataTime", content);

                    ctx.SaveChanges();
                    var factors = Parser.GetPollutantCodes(content);
                    foreach (var key in MasterFactors.Factors)
                    {
                        factors.Remove(key);
                    }

                    foreach (var factor in factors)
                    {
                        var factorCode = CommonInformation.GetMonitorFactorCodes(factor);
                        if (factorCode == null) continue;
                        var monitorDetail = monitorMain.RealDataMonitorDetails.FirstOrDefault(t => t.污染物编码ID == factorCode.ID) ;
                        if (monitorDetail == null)
                        {
                            monitorDetail = new RealDataMonitorDetail();
                            monitorMain.RealDataMonitorDetails.Add(monitorDetail);
                        }
                        monitorDetail.主实时监控ID = monitorMain.ID;
                        monitorDetail.污染物编码ID = factorCode.ID;
                        monitorDetail.分钟排放量 = Parser.GetFloatByKey(factor + "-Cou", content);
                    }
                    ctx.SaveChanges();
                }
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
        }
        public static void Process2061(string content)
        {
            try
            {
                var mn = Parser.GetStringByKey("MN", content);
                if (String.IsNullOrWhiteSpace(mn)) return;
                var mapping = CommonInformation.GetTableMappingByPkscMn(mn);
                if (mapping == null) return;
                using (var ctx = new PlatformContext())
                {
                    var monitorMain = ctx.RealDataMonitorMains.FirstOrDefault(t => t.企业基本信息ID == mapping.enterprise_id && t.设备信息ID == mapping.port_id) ;
                    if (monitorMain == null)
                    {
                        monitorMain = new RealDataMonitorMain();
                        ctx.RealDataMonitorMains.Add(monitorMain);
                    }
                    monitorMain.企业基本信息ID = mapping.enterprise_id;
                    monitorMain.设备信息ID = mapping.port_id;
                    monitorMain.污染物类别ID = 2;
                    if (monitorMain.在线状态ID == null || monitorMain.在线状态ID != 601)
                    {
                        monitorMain.在线状态ID = 601;
                        monitorMain.在离线状态时间 = Parser.GetDateTimeByDataTimeThenQnThenNow(content);
                    }

                    float? masterFactor = null;

                    foreach (var key in MasterFactors.Factors)
                    {
                        masterFactor = Parser.GetFloatByKey(key + "-Cou", content);
                        if (masterFactor != null)
                        {
                            break;
                        }
                    }

                    monitorMain.小时排放量 = masterFactor;
                    monitorMain.小时数据时间 = Parser.GetDateTimeByKey("DataTime", content);

                    ctx.SaveChanges();
                    var factors = Parser.GetPollutantCodes(content);
                    foreach (var key in MasterFactors.Factors)
                    {
                        factors.Remove(key);
                    }

                    foreach (var factor in factors)
                    {
                        var factorCode = CommonInformation.GetMonitorFactorCodes(factor);
                        if (factorCode == null) continue;
                        var monitorDetail = monitorMain.RealDataMonitorDetails.FirstOrDefault(t => t.污染物编码ID == factorCode.ID) ;
                        if (monitorDetail == null)
                        {
                            monitorDetail = new RealDataMonitorDetail();
                            monitorMain.RealDataMonitorDetails.Add(monitorDetail);
                        }
                        monitorDetail.主实时监控ID = monitorMain.ID;
                        monitorDetail.污染物编码ID = factorCode.ID;
                        monitorDetail.小时排放量 = Parser.GetFloatByKey(factor + "-Cou", content);
                    }
                    ctx.SaveChanges();
                }
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
        }
        public static void Process2031(string content)
        {
            try
            {
                var mn = Parser.GetStringByKey("MN", content);
                if (String.IsNullOrWhiteSpace(mn)) return;
                var mapping = CommonInformation.GetTableMappingByPkscMn(mn);
                if (mapping == null) return;
                using (var ctx = new PlatformContext())
                {
                    var monitorMain = ctx.RealDataMonitorMains.FirstOrDefault(t => t.企业基本信息ID == mapping.enterprise_id && t.设备信息ID == mapping.port_id) ;
                    if (monitorMain == null)
                    {
                        monitorMain = new RealDataMonitorMain();
                        ctx.RealDataMonitorMains.Add(monitorMain);
                    }
                    monitorMain.企业基本信息ID = mapping.enterprise_id;
                    monitorMain.设备信息ID = mapping.port_id;
                    monitorMain.污染物类别ID = 2;
                    if (monitorMain.在线状态ID == null || monitorMain.在线状态ID != 601)
                    {
                        monitorMain.在线状态ID = 601;
                        monitorMain.在离线状态时间 = Parser.GetDateTimeByDataTimeThenQnThenNow(content);
                    }

                    float? masterFactor = null;

                    foreach (var key in MasterFactors.Factors)
                    {
                        masterFactor = Parser.GetFloatByKey(key + "-Cou", content);
                        if (masterFactor != null)
                        {
                            break;
                        }
                    }

                    monitorMain.日排放量 = masterFactor;
                    monitorMain.日数据时间 = Parser.GetDateTimeByKey("DataTime", content);

                    ctx.SaveChanges();
                    var factors = Parser.GetPollutantCodes(content);
                    foreach (var key in MasterFactors.Factors)
                    {
                        factors.Remove(key);
                    }

                    foreach (var factor in factors)
                    {
                        var factorCode = CommonInformation.GetMonitorFactorCodes(factor);
                        if (factorCode == null) continue;
                        var monitorDetail = monitorMain.RealDataMonitorDetails.FirstOrDefault(t => t.污染物编码ID == factorCode.ID);
                        if (monitorDetail == null)
                        {
                            monitorDetail = new RealDataMonitorDetail();
                            monitorMain.RealDataMonitorDetails.Add(monitorDetail);
                        }
                        monitorDetail.主实时监控ID = monitorMain.ID;
                        monitorDetail.污染物编码ID = factorCode.ID;
                        monitorDetail.日排放量 = Parser.GetFloatByKey(factor + "-Cou", content);
                    }
                    ctx.SaveChanges();
                }
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
        }
        public static void Process2031ForMonth(string content)
        {
            try
            {
                var mn = Parser.GetStringByKey("MN", content);
                if (String.IsNullOrWhiteSpace(mn)) return;
                var mapping = CommonInformation.GetTableMappingByPkscMn(mn);
                if (mapping == null) return;
                var datatime = Parser.GetDateTimeByKey("DataTime", content);
                if (datatime == null) return;
                var begin = datatime.Value;
                begin = new DateTime(begin.Year, begin.Month, 1, 0, 0, 0);
                var end = begin.AddMonths(1);
                using (var ctx = new PlatformContext())
                {
                    var monitorMain = ctx.RealDataMonitorMains.FirstOrDefault(t => t.企业基本信息ID == mapping.enterprise_id && t.设备信息ID == mapping.port_id) ;
                    if (monitorMain == null)
                    {
                        monitorMain = new RealDataMonitorMain();
                        ctx.RealDataMonitorMains.Add(monitorMain);
                    }

                    monitorMain.企业基本信息ID = mapping.enterprise_id;
                    monitorMain.设备信息ID = mapping.port_id;
                    monitorMain.污染物类别ID = 2;
                    monitorMain.月数据时间 = datatime.Value;
                    monitorMain.月排放量 = ctx.DayDataMains.Where(t => t.监测时间 >= begin && t.监测时间 < end).Where(t => t.时段量 != null).Sum(t => t.时段量);

                    ctx.SaveChanges();
                    var ids = ctx.DayDataMains.Where(t => t.企业基本信息ID == mapping.enterprise_id && t.设备信息ID == mapping.port_id).Where(t => t.监测时间 >= begin && t.监测时间 < end).Select(t => t.ID);
                    var factors = Parser.GetPollutantCodes(content);
                    foreach (var key in MasterFactors.Factors)
                    {
                        factors.Remove(key);
                    }

                    foreach (var factor in factors)
                    {
                        var factorCode = CommonInformation.GetMonitorFactorCodes(factor);
                        if (factorCode == null) continue;
                        var monitorDetail = monitorMain.RealDataMonitorDetails.FirstOrDefault(t => t.污染物编码ID == factorCode.ID) ;
                        if (monitorDetail == null)
                        {
                            monitorDetail = new RealDataMonitorDetail();
                            monitorMain.RealDataMonitorDetails.Add(monitorDetail);
                        }
                        monitorDetail.主实时监控ID = monitorMain.ID;
                        monitorDetail.污染物编码ID = factorCode.ID;
                        monitorDetail.月排放量 = ctx.DayDataDetails.Where(t => ids.Contains(t.主日数据ID)).Where(t => t.污染物编码ID == factorCode.ID).Sum(t => t.Cou);

                    }
                    ctx.SaveChanges();
                }
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
        }
        public static void Process2031ForYear(string content)
        {
            try
            {
                var mn = Parser.GetStringByKey("MN", content);
                if (String.IsNullOrWhiteSpace(mn)) return;
                var mapping = CommonInformation.GetTableMappingByPkscMn(mn);
                if (mapping == null) return;
                var datatime = Parser.GetDateTimeByKey("DataTime", content);
                if (datatime == null) return;
                var begin = datatime.Value;
                begin = new DateTime(begin.Year, 1, 1, 0, 0, 0);
                var end = begin.AddYears(1);
                using (var ctx = new PlatformContext())
                {
                    var monitorMain = ctx.RealDataMonitorMains.FirstOrDefault(t => t.企业基本信息ID == mapping.enterprise_id && t.设备信息ID == mapping.port_id) ;
                    if (monitorMain == null)
                    {
                        monitorMain = new RealDataMonitorMain();
                        ctx.RealDataMonitorMains.Add(monitorMain);
                    }
                    monitorMain.企业基本信息ID = mapping.enterprise_id;
                    monitorMain.设备信息ID = mapping.port_id;
                    monitorMain.污染物类别ID = 2;
                    monitorMain.年数据时间 = datatime.Value;
                    monitorMain.年排放量 = ctx.DayDataMains.Where(t => t.监测时间 >= begin && t.监测时间 < end).Where(t => t.时段量 != null).Sum(t => t.时段量);

                    ctx.SaveChanges();
                    var ids = ctx.DayDataMains.Where(t => t.企业基本信息ID == mapping.enterprise_id && t.设备信息ID == mapping.port_id).Where(t => t.监测时间 >= begin && t.监测时间 < end).Select(t => t.ID);
                    var factors = Parser.GetPollutantCodes(content);
                    foreach (var key in MasterFactors.Factors)
                    {
                        factors.Remove(key);
                    }

                    foreach (var factor in factors)
                    {
                        var factorCode = CommonInformation.GetMonitorFactorCodes(factor);
                        if (factorCode == null) continue;
                        var monitorDetail = monitorMain.RealDataMonitorDetails.FirstOrDefault(t => t.污染物编码ID == factorCode.ID) ;
                        if (monitorDetail == null)
                        {
                            monitorDetail = new RealDataMonitorDetail();
                            monitorMain.RealDataMonitorDetails.Add(monitorDetail);
                        }
                        monitorDetail.主实时监控ID = monitorMain.ID;
                        monitorDetail.污染物编码ID = factorCode.ID;
                        monitorDetail.年排放量 = ctx.DayDataDetails.Where(t => ids.Contains(t.主日数据ID)).Where(t => t.污染物编码ID == factorCode.ID).Sum(t => t.Cou);

                    }
                    ctx.SaveChanges();
                }
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
        }
    }
}
